package com.gitee.qdbp.base.model.condition;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.gitee.qdbp.able.jdbc.ordering.Ordering;
import com.gitee.qdbp.able.jdbc.ordering.Orderings;

/**
 * 带排序的Where条件
 *
 * @author zhaohuihua
 * @version 160728
 */
public class OrderWhere<T> extends Where<T> {

    /** 版本序列号 **/
    private static final long serialVersionUID = 7716195898549898960L;

    /** 排序字段和排序类型 **/
    private List<Ordering> orderings;

    /** 附加数据 **/
    private Map<String, Object> extra;

    /** 获取排序字段列表 **/
    public List<Ordering> getOrderings() {
        return orderings;
    }

    /** 设置排序字段列表 **/
    public void setOrderings(List<Ordering> orderings) {
        this.orderings = orderings;
    }

    /** 以文本形式设置排序字段 **/
    public void setOrdering(String text) {
        this.orderings = Orderings.of(text);
    }

    /** 附加数据 **/
    public Map<String, Object> getExtra() {
        return extra;
    }

    /** 附加数据 **/
    public void setExtra(Map<String, Object> extra) {
        this.extra = extra;
    }

    /** 附加数据 **/
    public Object get(String key) {
        return this.extra == null ? null : this.extra.get(key);
    }

    /** 附加数据 **/
    public void put(String key, Object value) {
        if (this.extra == null) {
            this.extra = new HashMap<>();
        }
        this.extra.put(key, value);
    }

    /** 设置附加信息 **/
    public void put(Map<String, ?> extra) {
        if (this.extra == null) {
            this.extra = new HashMap<>();
        }
        this.extra.putAll(extra);
    }

    /** 转换 **/
    public static <T> OrderWhere<T> of(T where, List<Ordering> orderings) {
        OrderWhere<T> condition = new OrderWhere<>();
        condition.setWhere(where);
        condition.setOrderings(orderings);
        return condition;
    }

    /** 转换 **/
    public static <T> OrderWhere<T> of(T where, String ordering) {
        OrderWhere<T> condition = new OrderWhere<>();
        condition.setWhere(where);
        condition.setOrderings(Orderings.of(ordering));
        return condition;
    }
}
